module abird
import StdEnv, japi

/* Another animation, where the sprite is
static, and the background moves.
****************/

Start world
  # (js, world) = begin_japi world
    (sky, js) = jloadimage "sky.jpg" js
    (w, h, js) = imagesize sky js 
    (frm, js) = jnewFrame  (WH w 400) "Flying bird" js
    (cv, js) = jnewCanvas (XYWH 6 30 w 400) frm  js
    (img_a,js) = load_images js
    (frm, js) = jshow frm js
    js = loop frm 0 0 cv js with
            loop fr sel dx cv js
              | dx < ~w= loop fr sel 0 cv js
              # (x,js) = jsleep 250 js
              # (na,js) = jgetaction x js
              | na==(act fr)
                  # js= jdispose cv js
                  = jquit js
              # cv = jdraw (Img (dx,0) sky) cv
              # cv = jdraw (Img (dx+w, 0) sky) cv
              # cv = jdraw (Img(30,0) img_a.[sel]) cv
              # js= jsync js
              # sel = (sel+1) rem (size img_a)
              = loop fr sel (dx-10) cv js
  = end_japi js world
where
     load_images :: !JS -> (!{#Int},!*JS)
     load_images js
          # (b1,js) = jloadimage "bird1.gif" js
            (b2,js) = jloadimage "bird2.gif" js
            (b3,js) = jloadimage "bird3.gif" js
            (b4,js) = jloadimage "bird4.gif" js
          =   ({b1, b2, b3, b4},js)

 